<template>
  <SuperTable ref="superTableRef" search pager border align="center" :columns="columns" :data-request="fetchData"
    :export-config="{}" header-title="容器管理">
    <template #action>
      <Button type="primary" @click="addBoxFn">新增冻存盒</Button>
    </template>
  </SuperTable>
  <AddBoxModal v-model:show-modal="state.showAddBoxModal" @add-box-modal-ok="refreshTableFn" />
</template>

<script setup lang="ts">
import SuperTable from '@/framework/components/core/super-table/super-table.vue';
import AddBoxModal from '@/modules/underlying-data/pages/container-management/components/add-box-modal.vue';
import type { TableColumnItem } from './columns';
import { VxeGridPropTypes } from 'vxe-table';
import { baseColumns } from './columns';
import { reactive, ref } from 'vue';
import { Button, message } from 'ant-design-vue';
import { getRealBox, delBoxInfo, boxSearchInstance, boxSearchAll } from '@/api/base/box_data';
import { i18n } from '@/locales';
import { printBoxs } from '@/libs/print';

type State = {
  showAddBoxModal: boolean;
};

/**
 * data
 */
const state: State = reactive({
  showAddBoxModal: false,
}) as State;
const page: any = {};

/**
 * table
 */
const superTableRef = ref<InstanceType<typeof SuperTable>>();
const columns: TableColumnItem[] = [
  ...baseColumns,
  {
    title: '操作',
    field: '$action',
    hideInSearch: true,
    align: 'center',
    fixed: 'right',
    width: 250,
    actions: ({ row }) => [
      {
        label: '打印',
        onClick: () => { },
        popConfirm: {
          title: '确认打印标签吗？',
          onConfirm: () => {
            printBoxs([row]);
          },
        },
      },
      {
        label: '删除',
        popConfirm: {
          title: '确认删除该容器吗?',
          onConfirm: async () => {
            let params = {
              pageSize: page.pageSize,
              pageNum: page.pageNum,
              orderBy: [
                {
                  field: 'id',
                  order: 'desc',
                },
              ],
              serial: row.serial,
              epc: row.epc,
            };
            const res = await boxSearchInstance(params, row.id);
            if (res.result === 0) {
              await delBoxInfo(row.id);
              message.success('删除成功！');
              refreshTableFn();
            } else {
              message.warn(
                // @ts-ignore
                `该${i18n.global.t('specimen.Type_of_test_tube_box')}内已经有${i18n.global.t('specimen.test_tube')},无法删除`,
              );
            }
          },
        },
      },
    ],
  },
];
const fetchData = async (ori: VxeGridPropTypes.ProxyAjaxQueryParams) => {
  let data = superTableRef.value?.getFormModel();
  page.pageSize = ori.page.pageSize;
  page.pageNum = ori.page.currentPage;

  let params: API.SharedDataType = {
    pageSize: ori.page.pageSize,
    pageNum: ori.page.currentPage,
    orderBy: [
      {
        field: 'id',
        order: 'desc',
      },
    ],
    ...data,
    virtualBoxId: -1,
  };
  let res = await getRealBox(params);

  return res.result;
};
const refreshTableFn = () => {
  superTableRef.value?.refreshTable();
};

/**
 * fn
 */
const addBoxFn = () => {
  state.showAddBoxModal = true;
};
</script>

<style scoped lang="less"></style>
